using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Xml;
namespace Microsoft.Phone.Controls.Maps.Core
{
	public static class MapConfiguration
	{
		private static MapConfigurationProvider configuration;
		private static string defaultServiceUriFormat = "{UriScheme}://dev.virtualearth.net/webservices/v1/MapControlConfigurationService/MapControlConfigurationService.svc/binaryHttp";
		public static event EventHandler<MapConfigurationLoadedEventArgs> Loaded
		{
			[MethodImpl(32)]
			add
			{
				MapConfiguration.Loaded = (EventHandler<MapConfigurationLoadedEventArgs>)Delegate.Combine(MapConfiguration.Loaded, value);
			}
			[MethodImpl(32)]
			remove
			{
				MapConfiguration.Loaded = (EventHandler<MapConfigurationLoadedEventArgs>)Delegate.Remove(MapConfiguration.Loaded, value);
			}
		}
		public static void SetServiceUri(Uri address)
		{
			MapConfiguration.LoadConfiguration(new MapConfigurationFromService(address));
		}
		public static void Load(XmlReader config)
		{
			MapConfiguration.LoadConfiguration(new MapConfigurationFromFile(config));
		}
		public static void Load(Uri address)
		{
			MapConfiguration.LoadConfiguration(new MapConfigurationFromWeb(address));
		}
		public static void GetSection(string version, string sectionName, string culture, MapConfigurationCallback callback)
		{
			MapConfiguration.GetSection(version, sectionName, culture, callback, null);
		}
		public static void GetSection(string version, string sectionName, string culture, MapConfigurationCallback callback, object userState)
		{
			if (MapConfiguration.configuration == null)
			{
				if (DesignerProperties.get_IsInDesignTool())
				{
					try
					{
						Stream manifestResourceStream = typeof(MapConfiguration).get_Assembly().GetManifestResourceStream("Microsoft.Phone.Controls.Maps.Data.DesignConfig.xml");
						if (manifestResourceStream != null)
						{
							XmlReader config = XmlReader.Create(manifestResourceStream);
							MapConfiguration.LoadConfiguration(new MapConfigurationFromFile(config));
						}
						goto IL_68;
					}
					catch (XmlException)
					{
						goto IL_68;
					}
				}
				Uri address = new Uri(MapConfiguration.defaultServiceUriFormat.Replace("{UriScheme}", "HTTP"), 1);
				MapConfiguration.LoadConfiguration(new MapConfigurationFromService(address));
			}
			IL_68:
			if (string.IsNullOrEmpty(version))
			{
				throw new ArgumentException(ExceptionStrings.MapConfiguration_GetSection_NonNull, "version");
			}
			if (string.IsNullOrEmpty(sectionName))
			{
				throw new ArgumentException(ExceptionStrings.MapConfiguration_GetSection_NonNull, "sectionName");
			}
			MapConfiguration.configuration.GetConfigurationSection(version, sectionName, culture, callback, userState);
		}
		internal static void Reset()
		{
			MapConfiguration.Loaded = null;
			if (MapConfiguration.configuration != null)
			{
				MapConfiguration.configuration.Loaded -= new EventHandler<MapConfigurationLoadedEventArgs>(MapConfiguration.provider_Loaded);
				MapConfiguration.configuration = null;
			}
		}
		private static void LoadConfiguration(MapConfigurationProvider provider)
		{
			if (MapConfiguration.configuration != null)
			{
				throw new ConfigurationNotLoadedException(ExceptionStrings.ConfigurationException_InvalidLoad);
			}
			MapConfiguration.configuration = provider;
			provider.Loaded += new EventHandler<MapConfigurationLoadedEventArgs>(MapConfiguration.provider_Loaded);
			provider.LoadConfiguration();
		}
		private static void provider_Loaded(object sender, MapConfigurationLoadedEventArgs e)
		{
			EventHandler<MapConfigurationLoadedEventArgs> loaded = MapConfiguration.Loaded;
			if (loaded != null)
			{
				loaded.Invoke(null, e);
			}
		}
	}
}
